package com.kaifamiao.ioc.creation;

import org.springframework.beans.factory.FactoryBean;

import java.util.Calendar;
import java.util.Date;

public class DateFactoryBean implements FactoryBean<Date> {

    private int year;
    private int month;
    private int date;
    private int hours;
    private int minutes;
    private int seconds;

    @Override
    public Date getObject() throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.clear(); // 清除所有日历字段
        cal.set( year, month-1, date, hours, minutes, seconds );
        Date date = cal.getTime();
        return date;
    }

    @Override
    public Class<?> getObjectType() {
        return Date.class;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        if( month < 1 || month > 12 ) {
            throw new IllegalArgumentException("月份必须介于1到12之间");
        }
        this.month = month;
    }

    public int getDate() {
        return date;
    }

    public void setDate(int date) {
        this.date = date;
    }

    public int getHours() {
        return hours;
    }

    public void setHours(int hours) {
        this.hours = hours;
    }

    public int getMinutes() {
        return minutes;
    }

    public void setMinutes(int minutes) {
        this.minutes = minutes;
    }

    public int getSeconds() {
        return seconds;
    }

    public void setSeconds(int seconds) {
        this.seconds = seconds;
    }

}
